import requests
from pymongo import MongoClient
# import cfscrape

# api_url = 'https://api.securitytrails.com/v1/history/{hostname}/dns/{type}'
# api_key = 'ohcvr4S7P0Dpz71a2PGopcBQoLEMdeUB'
api_key = '7YxY2U5IdssFd3mq8AIvKZoOmdIBpyfM'

client = MongoClient('mongodb://lxt:aa347347@173.82.107.218:27017/?authSource=admin')
db = client['dns']
collection_domains = db['domains']
collection_history = db['history']

for domain in collection_domains.find():
    hostname = domain['name']
    if collection_history.count_documents({
        'endpoint': f'/v1/history/{hostname}/dns/a'
    }) == 0:
        qtype = 'a'
        res = requests.get(f'https://api.securitytrails.com/v1/history/{hostname}/dns/{qtype}',
                           headers={"accept": "application/json",
                                    'apiKey': api_key})
        data = res.json()
        data['name'] = hostname
        collection_history.insert_one(data)
